package org.autumn.review.thread;

public class Disorder {
    static boolean flag = false;
    static int num = 0;

    public static void main(String[] args) throws InterruptedException {
        int count = 0;
        for (; ; ) {
            Thread t1 = new Thread(() -> {
                if (flag) {
                    num += num;
                } else {
                    num = 1;
                }
            }, "t1");

            Thread t2 = new Thread(() -> {
                num = 2;
                flag = true;
            }, "t2");
            t2.start();
            t1.start();

            t1.join();
            t2.join();
            // num = 1  num= 4 num =2

            if (num == 0) {
                System.err.println(count + ": " + num);
                break;
            } else if (num == 2){
                System.err.println(count + ": mk" + num);
            }else {
                System.out.println(count + ": " + num);
            }
            num = 0;
            flag = false;
            count++;
        }

    }
}
